#include "metadata.h"
#include "collection.h"


#include <QDebug>
#include <QTimer>


MetaData::MetaData(Collection *parent) :
    QObject(parent),
    d(new Data())
{
}

MetaData::MetaData(const MetaData &other) :
    QObject(other.parent())
{
    d->items = other.items();
}

void MetaData::addItem(MetaItem *item)
{
    d->items.append(item);
}

void MetaData::insertItem(int index, MetaItem *item)
{
    d->items.insert(index, item);
}

void MetaData::addItem(const QString& name)
{
    addItem(new MetaItem(name, this));
}

void MetaData::insertItem(int index, const QString &name)
{
    insertItem(index, new MetaItem(name, this));
}

void MetaData::renameItem(int index, const QString &name)
{
    MetaItemPtr item = d->items.value(index, 0);
    if (item.isNull()) return;

    item->setField(name);
}

void MetaData::moveItem(int from, int to)
{
    d->items.move(from, to);
}

void MetaData::removeItem(int index)
{
    if (index < 0 || index >= d->items.count()) return;

    MetaItem* item = d->items.takeAt(index);
    if (!item) return;

    item->setParent(0);
    item->deleteLater();
}

const QList<MetaItemPtr> &MetaData::items() const
{
    return d->items;
}

MetaItemPtr MetaData::item(int index) const
{
    return d->items.value(index, 0);
}

int MetaData::indexOf(MetaItem *item) const
{
    return d->items.indexOf(item);
}

